package com.hxiaoyi.cloud.controller;

import com.hxiaoyi.cloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 描述
 * DefaultProperties配置全局的降级方法
 * @author 黄敬意
 * @date 2022-02-12 16:03
 **/
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentGlobalFallbackMethod")
public class PaymentController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    /**ok*/
    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfoOK(id);
        log.info("-------返回的result为：" + result);
        return result;
    }

//    @HystrixCommand(fallbackMethod = "paymentTimeoutHandler", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
//    })
    /**超时*/
    @GetMapping(value = "/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand
    public String paymentInfoTimeOut(@PathVariable("id") Integer id) {
        //int a = 10/0;
        String result = paymentHystrixService.paymentInfoTimeOut(id);
        log.info("-------返回的超时result为：" + result);
        return result;
    }

    /**
     * Hystrix出错兜底方法
     * @param id
     * @return String
     */
    public String paymentTimeoutHandler(@PathVariable("id") Integer id){
        return "我是消费者小易，外部接口返回错误，请稍后再试呜呜呜--/(ㄒoㄒ)/~~," + "id = " +id;
    }
    /**
     * Hystrix全局出错兜底方法
     * @return String
     */
    public String paymentGlobalFallbackMethod(){
        return "系统繁忙，请稍后再试呜呜呜--/(ㄒoㄒ)/~~,";
    }
}
